//
//  DDNetWorkTools.m
//  AFNetWorking第三方库 以及封装
//
//  Created by rimi on 16/9/5.
//  Copyright © 2016年 rimi. All rights reserved.
//

#import "DDNetWorkTools.h"

@implementation DDNetWorkTools
static DDNetWorkTools* dDNetWorkTools;//创建一个静态的网络工具单例对象指针

#pragma mark 设计单例的方法
+ (instancetype)shareDDNetWorkTools{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
         dDNetWorkTools = [[DDNetWorkTools alloc]init];
        
    });
   
    return dDNetWorkTools;
    

}
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
//    [super allocWithZone:zone];
    @synchronized (self) {
        if (!dDNetWorkTools) {
            dDNetWorkTools = [super allocWithZone:zone];
        }
        return dDNetWorkTools;
    }
   
}
- (id)copyWithZone:(NSZone *)zone{
    return dDNetWorkTools;
}

#pragma mark 请求数据的方法
- (void)requestWithMethod:(RequestMethod)requestMelhed Url:(NSString*)url Parameters:(id)parameters finished:(DataAndErrorBlock)dataAndErrorBlock{
    if (requestMelhed ==GET) {
     [self GET:url parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
         dataAndErrorBlock(responseObject,nil);
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
           dataAndErrorBlock(nil,error);
     }];

    }else{
        [self POST:url parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
             dataAndErrorBlock(responseObject,nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            dataAndErrorBlock(nil,error);
        }];
 
    
    }
#pragma mark block里不能使用return 返回数据 因此定义了一个block用来返回数据和错误信息


}
@end
